package flex.cc.system.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.system.dao.DictTypeDao;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.entity.DictTypeEntity;
import flex.cc.system.service.DictCodeService;
import flex.cc.system.service.DictTypeService;
import flex.cc.system.vo.DictTypeVo;


@Service("dictTypeService")
public class DictTypeServiceImpl extends ServiceImpl<DictTypeDao, DictTypeEntity> implements DictTypeService {

	@Autowired
    private DictCodeService dictCodeService;
	
	/**
	 * 根据页面所传条件查询数据字典类型信息
	 */
	@Override
	public IPage<DictTypeEntity> searchAllType(DictTypeEntity dictTypeEntity) {
		QueryWrapper<DictTypeEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(PublicUtil.isNotEmpty(dictTypeEntity.getType()), "type", dictTypeEntity.getType())
					.like(PublicUtil.isNotEmpty(dictTypeEntity.getTypeDesc()), "type_desc", dictTypeEntity.getTypeDesc())
					.eq(PublicUtil.isNotEmpty(dictTypeEntity.getComCode()),"com_code", dictTypeEntity.getComCode())
					.eq("valid_status", "1")  //启用状态
					.orderByDesc("created_time");
		IPage<DictTypeEntity> page = this.page(new Page(dictTypeEntity.getPageNum(),dictTypeEntity.getPageSize()), queryWrapper);
		return page;
	}

	/**
	 * 保存或修改数据字典主类型
	 */
	@Override
	public String saveOrUpdateDictType(DictTypeVo vo,LoginAuthDto loginAuthDto) {
		QueryWrapper<DictTypeEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", vo.getType())
					.eq("com_code", vo.getComCode())
					.eq("valid_status", "1"); //启用状态
		List<DictTypeEntity> list = this.list(queryWrapper);
		vo.setUpdateInfo(loginAuthDto);
		DictTypeEntity dictTypeEntity = new DictTypeEntity();
		BeanUtils.copyProperties(vo,dictTypeEntity);
		if(PublicUtil.isEmpty(list)) {//新增type类型
			dictTypeEntity.setValidStatus("1");
			this.save(dictTypeEntity);
			return "新增type成功";
		}else {//已存在
			for (DictTypeEntity dictTypeEntity2 : list) {
				if(!dictTypeEntity2.getId().equals(dictTypeEntity.getId())) {
					return "此类型已存在，不允许新增";
				}
			}
			this.updateById(dictTypeEntity);
			return "修改type成功";
		}
	}

	/**
	 * 逻辑删除数据字典主类型 页面先调用searchDictTypeById方法查询该type没有下级时才可以删除
	 */
	@Override
	public boolean deleteDictTypeById(Long id, LoginAuthDto loginAuthDto) {
		DictTypeEntity dictTypeEntity = this.getById(id);
		boolean deleteFlag = true;
		if(!PublicUtil.isEmpty(dictTypeEntity)) {
			List<DictCodeEntity> searchCodeByTypeList = dictCodeService.searchCodeByTypeList(dictTypeEntity.getType(),0,dictTypeEntity.getComCode());
			if(!PublicUtil.isEmpty(searchCodeByTypeList)) {//有下级 不能删除
				deleteFlag = false;
			}
		}
		if(deleteFlag) {
			//把操作座席的工号更新上再删除
			dictTypeEntity.setUpdateInfo(loginAuthDto);
			this.updateById(dictTypeEntity);
			this.removeById(id);
		}
		return deleteFlag;
	}

	 

}